import javax.swing.*;
import javax.swing.plaf.ComponentUI;
import java.awt.*;
import java.awt.geom.Rectangle2D;

public class MyComponent extends JComponent {
    public MyComponent() {
        super();
        setUI(new ComponentUI(){});	//设定一个UI
        setOpaque(true);//将component设置为不透明
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);//调用super的paintComponent方法
        Graphics2D g2 = (Graphics2D)g;

        //画矩形
        Rectangle2D rect = new Rectangle2D.Double(100,100,200,150);
        g2.setPaint(Color.RED);	//设置颜色
        g2.fill(rect);	//画图
    }
}
